package com.zhongshengnetwork.rightbe.lang.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.FragmentViewPageAdapter;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.lang.view.ChannelFragment;
import com.zhongshengnetwork.rightbe.lang.view.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.List;

public class ChannelHomeActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {
    private TopBarView topbar;
    private String titleString;
    private List<String>titleList = null;
    private String typeString;
    private List<String>typeList = null;

    private ViewPagerIndicator viewPagerIndicator;
    private ViewPager mPaper;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private int select = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleString = getIntent().getStringExtra("title");
        if (!CommonUtils.isEmpty(titleString)){
            titleList = CommonUtils.stringToArrayList(titleString);
        }
        typeString = getIntent().getStringExtra("type");
        if (!CommonUtils.isEmpty(typeString)){
            typeList = CommonUtils.stringToArrayList(typeString);
        }

        select = getIntent().getIntExtra("select",0);
        setContentView(R.layout.activity_channel_home);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.setClickListener(this);
        float density = ScreenUtils.getScreenDensity(ChannelHomeActivity.this);
        topbar.getRightView().setImageResource(R.drawable.searchicon);
        ViewGroup.LayoutParams params = topbar.getRightTextView().getLayoutParams();
        params.width = (int) (27*density);
        params.height = (int) (27*density);
        topbar.getRightTextView().setLayoutParams(params);
        topbar.getRightTextView().setVisibility(View.VISIBLE);
        topbar.getRightTextView().setClickable(true);
        topbar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChannelHomeActivity.this, LangSearchActivity.class);
                startActivity(intent);
            }
        });
        topbar.getTitleView().setClickable(true);
        topbar.getTitleView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFragments != null && mFragments.size() > 0){
                    ChannelFragment channelFragment = (ChannelFragment)mFragments.get(mPaper.getCurrentItem());
                    channelFragment.getFirstData(true);
                }

            }
        });

        viewPagerIndicator = (ViewPagerIndicator) findViewById(R.id.indicator);
        mPaper = (ViewPager)findViewById(R.id.viewpager);
        if (typeList != null && typeList.size() > 0){
            for (int i = 0; i < typeList.size(); i++){
                ChannelFragment channelFragment = new ChannelFragment();
                channelFragment.channelType = Integer.valueOf(typeList.get(i));
                if(i > 0){
                    channelFragment.tip = "搜索"+titleList.get(i)+"文案";
                }
                mFragments.add(channelFragment);
            }
        }
        Log.e("TAG","mFragments大小："+mFragments.size());

        FragmentViewPageAdapter fragmentViewPageAdapter = new FragmentViewPageAdapter(
                getSupportFragmentManager(), mFragments){
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mFragments.get(arg0);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titleList.get(position);
            }
        };
        mPaper.setAdapter(fragmentViewPageAdapter);
        viewPagerIndicator.setViewPager(mPaper);
        mPaper.setCurrentItem(select,true);
        //fragmentViewPageAdapter.notifyDataSetChanged();

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LangDetailActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

}
